package com.capgemini.teamtwo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.capgemini.teamtwo.common.BusinessException;
import com.capgemini.teamtwo.common.ExceptionEnum;
import com.capgemini.teamtwo.entity.ProjectInfo;
import com.capgemini.teamtwo.entity.ProjectSchedule;
import com.capgemini.teamtwo.entity.vo.ProjectScheduleVO;
import com.capgemini.teamtwo.mapper.ProjectScheduleMapper;
import com.capgemini.teamtwo.service.ProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProjectScheduleServiceImpl extends ServiceImpl<ProjectScheduleMapper, ProjectSchedule> implements com.capgemini.teamtwo.service.ProjectScheduleService {

    @Resource
    private ProjectScheduleMapper projectScheduleMapper;


    /**
     * 根据id查询项目计划
     *
     * @param projectId 项目id
     * @return 项目计划详细信息
     */
    @Override
    public List<ProjectSchedule> findScheduleById(Integer projectId) {
        // 判断参数是否为空
        if (projectId == null) {
            throw new BusinessException(ExceptionEnum.PARAMETER_EMPTY);
        }
        List<ProjectSchedule> projectSchedule = projectScheduleMapper.findScheduleById(projectId);
        if (projectSchedule == null) {
            throw new BusinessException(ExceptionEnum.PARAMETER_EMPTY);
        }
        return projectSchedule;
    }

    /**
     * 新增项目计划功能
     *
     * @param projectScheduleVO 前端表单信息
     */
    @Override
    public ProjectSchedule insertProjectSchedule(ProjectScheduleVO projectScheduleVO) {
        // 判断参数是否为空
        if (projectScheduleVO == null) {
            throw new BusinessException(ExceptionEnum.PARAMETER_EMPTY);
        }
        List<String> projectDate = projectScheduleMapper.findDate(projectScheduleVO.getProjectId());
        if (projectDate.contains(projectScheduleVO.getProjectDate())){
            // 抛异常
            throw new BusinessException(ExceptionEnum.DATA_EXCEPTION);
        }

        ProjectSchedule projectSchedule = new ProjectSchedule();
        projectSchedule.setProjectId(projectScheduleVO.getProjectId());
        projectSchedule.setProjectDate(projectScheduleVO.getProjectDate());
        projectSchedule.setPlan(projectScheduleVO.getPlan());
        projectSchedule.setActual(projectScheduleVO.getActual());
        // 实际的完成量要小于计划量
        if (projectScheduleVO.getPlan() < projectScheduleVO.getActual()) {
            throw new BusinessException(ExceptionEnum.SCHEDULE_EXCEPTION);
        }
        Integer rows = projectScheduleMapper.insertProjectSchedule(projectSchedule);
        // 计算项目进度并保存
        if (rows != 1) {
            throw new BusinessException(ExceptionEnum.INSERT_EXCEPTION);
        }
        return projectSchedule;
    }

    /**
     * 修改项目计划功能
     *
     * @param projectScheduleVO 前端表单信息
     */
    @Override
    public ProjectSchedule updateProjectSchedule(ProjectScheduleVO projectScheduleVO) {
        // 判断参数是否为空
        if (projectScheduleVO == null) {
            throw new BusinessException(ExceptionEnum.PARAMETER_EMPTY);
        }
        ProjectSchedule projectSchedule = new ProjectSchedule();
        projectSchedule.setProjectId(projectScheduleVO.getProjectId());
        projectSchedule.setProjectDate(projectScheduleVO.getProjectDate());
        projectSchedule.setPlan(projectScheduleVO.getPlan());
        projectSchedule.setId(projectScheduleVO.getId());
        projectSchedule.setActual(projectScheduleVO.getActual());
        // 实际的完成量要小于计划量
        if (projectScheduleVO.getPlan() < projectScheduleVO.getActual()) {
            throw new BusinessException(ExceptionEnum.SCHEDULE_EXCEPTION);
        }
        Integer rows = projectScheduleMapper.updateById(projectSchedule);
        // 计算项目进度并保存
        if (rows != 1) {
            throw new BusinessException(ExceptionEnum.INSERT_EXCEPTION);
        }
        return projectSchedule;
    }

    /**
     * 计算项目进度
     *
     * @return 成功或失败
     */
//    @Override
//    public boolean calculateAndUpdate(Integer projectId) {
//
//        //计算项目进度并保存
//        ProjectInfo projectInfo = projectInfoService.getById(projectId);
//        if (projectInfo != null) {
//            LambdaQueryWrapper<ProjectSchedule> projectScheduleLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            projectScheduleLambdaQueryWrapper.eq(ProjectSchedule::getProjectId, projectInfo.getId());
//            List<ProjectSchedule> actualWork = this.list(projectScheduleLambdaQueryWrapper);
//            Integer actualTotal = actualWork.stream().mapToInt(ProjectSchedule::getActual).sum();
//            Integer planTotal = actualWork.stream().mapToInt(ProjectSchedule::getPlan).sum();
//            if (planTotal != 0) projectInfo.setRate((actualTotal / planTotal) * 100);
//            else
//                projectInfo.setRate(0);
//            projectInfoService.updateById(projectInfo);
//        }
//        //end
//        return true;
//    }
}
